package cz.idsv.web.mvc.model.dao;

import java.util.Collection;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.idsv.web.mvc.model.entity.RegistrationEntity;


/**
 * Registration DAO implementation.
 * 
 * @author Jan Borzensky
 */

@Repository("RegistrationDaoImpl")
@Transactional(readOnly = true)
public class RegistrationDaoImpl implements RegistrationDao {

	protected HibernateTemplate template = null;

	/**
	 * Sets Hibernate session factory.
	 */
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		template = new HibernateTemplate(sessionFactory);
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void save(RegistrationEntity registrationEntity) {
		template.saveOrUpdate(registrationEntity);
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public void delete(RegistrationEntity registrationEntity) {
		template.delete(registrationEntity);
	}

	public RegistrationEntity findRegistrationById(Integer id) throws DataAccessException{
		return template.get(RegistrationEntity.class, id);
		
	
	}

	@SuppressWarnings("unchecked")
	public Collection<RegistrationEntity> findRegistration() {
		return template.find("from Registration");
	}

	@SuppressWarnings("unchecked")
	public Collection<RegistrationEntity> findRegistrationByName(String name)
			throws DataAccessException {
		return template.find("from Registration a where a.name = ?", name);
	}

}
